/*
 * Analyze.cpp
 *
 *  Created on: Oct 4, 2010
 *      Author: gibson
 */

#include "Analyze.h"
#include "DataReader.h"
#include "TH2D.h"
#include "TH1I.h"
#include "TCanvas.h"
#include <iostream>
#include "TPad.h"
#include "TApplication.h"
#include "globals.hh"
#include "TFile.h"
#include "PhotonCounter.h"

using namespace std;

void TCanvas::Closed()
{
        Emit("Closed()");

        gApplication->Terminate(0);
}

Analyze::Analyze(char* path)
{
	CrystalDistribution(path);
}

Analyze::~Analyze()
{
	;
}

void Analyze::CrystalDistribution(char* path)
{
	dataHisto = new TH2D("dataHisto","dataHisto",72,-0.9*m,0.9*m,72,-0.9*m,0.9*m);
	photonHisto = new TH1I("Gammas per Klong","Gammas Incident on Detector per Klong",15,0,15);
	dataReader = new DataReader(path);
	proceed = dataReader->ReadNext(dataHisto);

	while(proceed)
	{
		count = 0;
		for(int l=0 ; l<3 ; l++)
		{
			count += PhotonCounter(dataHisto);

			delete dataHisto;
			dataHisto = new TH2D("dataHisto","dataHisto",72,-0.9*m,0.9*m,72,-0.9*m,0.9*m);
			proceed = dataReader->ReadNext(dataHisto);
		}

		photonHisto->Fill(count);
	}

	delete dataHisto;
	delete dataReader;

	TCanvas* canvas = new TCanvas("canvas","Number of Crystals Hit per Klong Event",500,400);
	photonHisto->Draw();

	TFile* file = new TFile("./output.root","recreate");
	photonHisto->Write();
	file->Close();

	gApplication->Run(true);

	delete photonHisto;
}
